package mysql

import "gorm.io/gorm"

type Order struct {
	gorm.Model
	UserId int64 `gorm:"type:int"`
	GoodsIds
	OrderSn string  `gorm:"type:varchar(36);not null; unique"`
	Price   float64 `gorm:"type:decimal(10,2)"`
	Status  int64   `gorm:"type:int;default:1;"` // 1 待支付 2 已支付 3 已取消 4 已完成
}
type GoodsIds struct {
	GoodsId int64 `gorm:"type:int;"`
}

func (o *Order) CreateOrder(userId int64, goodsId int64, OrderSn string, price float64) error {
	order := &Order{
		UserId: userId,
		GoodsIds: GoodsIds{
			GoodsId: goodsId,
		},
		OrderSn: OrderSn,
		Price:   price,
	}
	err := DB.Create(&order).Error
	if err != nil {
		return err
	}
	return nil
}
func (o *Order) OrderList() (order []*Order, err error) {
	err = DB.Find(&order).Error
	if err != nil {
		return nil, err
	}
	return order, nil
}
